package com.rljk.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rljk.common.consts.AssetConst;
import com.rljk.domain.dto.SignList;
import com.tencent.trustsql.sdk.TrustSDK;
import com.tencent.trustsql.sdk.util.SignStrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;

import java.util.*;

/**
 * Author:   YASUO
 * Date:     2018/7/28 14:52
 * Description:
 */
@Slf4j
public class TrustSqlUtil {


    /**
     * 获取账户
     * @param accountCertFetchListResult
     * @return
     */
    public static String getAccount(String accountCertFetchListResult){
        JSONObject jsonSrcUser = JSONObject.parseObject(accountCertFetchListResult);
        JSONArray jsonArrSrcUser = jsonSrcUser.getJSONArray("retdata");
        String account	 = jsonArrSrcUser.getJSONObject(0).getString("account_address");
        return account;
    }

    /**
     * 验签
     * @param paramMap
     * @param resultJson
     * @return
     */
    public static String verifyString(TreeMap<String, Object>paramMap, JSONObject resultJson){
        try {
            if ("0".equals(resultJson.getString("retcode"))&&"OK".equals(resultJson.getString("retmsg")) ) {
                // 验证返回数据的mch_sign
                paramMap.clear();
                for (Map.Entry<String, Object> entry : resultJson.entrySet()) {
                    if (!"mch_sign".equals(entry.getKey())) {
                        paramMap.put(entry.getKey(), entry.getValue());
                    }
                }
                log.info("分析paramMap:"+paramMap.toString());
                boolean mchSignValid = TrustSDK.verifyString(AssetConst.COMMUNICATION_PUBLIC_KEY,
                        SignStrUtil.mapToKeyValueStr(paramMap), resultJson.getString("mch_sign"));
                resultJson.put("mch_sign_verify", mchSignValid);
                log.info("验签mch_sign_verify:"+mchSignValid);
                return resultJson.toJSONString();
            }
        }catch (Exception e){
            e.getStackTrace();
        }
        return  null;
    }


    /**
     * 获取签名列表
     * @param sign_str_list
     * @return
     */
    public static JSONArray getSignList(JSONArray sign_str_list)throws Exception{
        for(int i=0;i<sign_str_list.size();i++){
            JSONObject json = sign_str_list.getJSONObject(i);
            String sign = TrustSDK.SignRenString("vL/lFwaGGRxG0ND13xu1XxQK7qP6ny3Kt3LLLKWn8kM=",
                    Hex.decodeHex(json.getString("sign_str").toCharArray()));
            log.info("完成待签名串"+sign);
            boolean flag = TrustSDK.verifyRenSign(AssetConst.PUBLISH_PUBLIC_KEY,
                    Hex.decodeHex(json.getString("sign_str").toCharArray()),
                    sign);
            System.out.println("待签名串验签结果flag:" + flag);
            json.put("sign",sign);	//使用发行方的私钥对待签名串进行签名,调用sdk的SignRenString函数
        }
        log.info("获取签名列表sign_list:"+sign_str_list);
        return sign_str_list;
    }


    /**
     * 获取签名列表
     * @param sign_str_list
     * @return
     */
    public static JSONArray getSignList1(JSONArray sign_str_list)throws Exception{
        JSONArray jsonArray = new JSONArray();
        for(int i=0;i<sign_str_list.size();i++){
            JSONObject json = sign_str_list.getJSONObject(i);
            String sign = TrustSDK.SignRenString(AssetConst.PUBLISH_PRIVATE_KEY,
                    Hex.decodeHex(json.getString("sign_str").toCharArray()));
            log.info("完成待签名串"+sign);
            boolean flag = TrustSDK.verifyRenSign(AssetConst.PUBLISH_PUBLIC_KEY,
                    Hex.decodeHex(json.getString("sign_str").toCharArray()),
                    sign);
            System.out.println("待签名串验签结果flag:" + flag);

            SignList signList= new SignList();
            signList.setId(json.getString("id"));
            signList.setAccount(json.getString("account"));
            signList.setSign_str(json.getString("sign_str"));
            signList.setSign(sign);
            jsonArray.add(signList);
        }
        log.info("获取签名列表sign_list:"+jsonArray.toString());
        log.info("获取签名列表sign_list:"+jsonArray);
        return jsonArray;
    }

    /**
     * 通讯方进行签名
     * @param paramMap
     * @return
     */
    public static String getCommunicationSign(TreeMap<String, Object> paramMap)throws Exception{
        String signString = TrustSDK.signString(AssetConst.COMMUNICATION_PRIVATE_KEY, SignStrUtil.mapToKeyValueStr(paramMap).getBytes("UTF-8"), false);
        log.info("通讯方进行签名signString:"+signString);
        return signString;
    }





    public static void main(String[] args) throws Exception {
        JSONArray sign_str_list =new JSONArray();
        JSONObject json = new JSONObject();
        json.put("id","1");
        json.put("account","account");
        json.put("sign_str","sign_str");
        json.put("sign","sign");
        sign_str_list.add(json);
        System.out.println("sign_str_list:"+sign_str_list);
        Object signList = getSignList1(sign_str_list);
        System.out.println("signList:"+signList);

    }



}
